<?php

namespace App\Http\Controllers;

use App\Models\ApiLog;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $user;

    public function __construct()
    {
        $this->user = Auth::guard('api')->user();
    }
    /**
     * 成功
     * @param string $msg
     * @param $data
     * @param $code
     * @return \Illuminate\Http\JsonResponse
     */
    public function success($msg = 'success', $data = null, $code = 200)
    {
        ApiLog::addLog(0, $code, $msg, $data);
        return response()->json([
            'code'  => $code,
            'msg'   => $msg,
            'data'  => $data
        ]);
    }

    /**
     * 失败
     * @param string $msg
     * @param $data
     * @param $code
     * @return \Illuminate\Http\JsonResponse
     */
    public function error($msg = 'error', $data = null, $code = 400)
    {
        ApiLog::addLog(0, $code, $msg, $data);
        return response()->json([
            'code'  => $code,
            'msg'   => $msg,
            'data'  => $data
        ]);
    }
}
